<?php
/**
 * Holds the navigation system
 *
 * @category CC
 * @package CC
 * @subpackage Navigation
 */
class CC_Navigation
{
    /**
     * Singleton object for this class
     *
     * @var CC_Navigation
     * @access protected
     */
    protected static $_instance;
    
    /**
     * Holds the navigation array
     * 
     * @var array
     * @access protected
     */
    protected $_navigation;
    
    /**
     * Constructs the Navigation object. Must not be called directly
     * 
     * @access public
     * @return void
     */
    protected function __construct ()
    {
        $this->_navigation = $this->_markActive($this->_filter($this->_getPages()));
    }
    
    /**
     * Returns a singleton instance of the class
     * 
     * @access public
     * @return void
     */
    public function getInstance ()
    {
        if (NULL === self::$_instance) 
        {
            self::$_instance = new CC_Navigation();
        }
        
        return self::$_instance;
    }
    
    /**
     * Implements the __clone() magic method, forbidding the cloning
     * of this object
     * 
     * @access public
     * @return void
     */
    public function __clone ()
    {
        throw new Zend_Exception('Cloning singleton objects is forbidden');
    }
    
    /**
     * Returns an array of pages
     * 
     * @access protected
     * @return void
     */
    protected function _getPages ()
    {
        $pages = array(
            array(
                'label' => 'Backoffice',
                'pages' => array(
                    array(
                        'label' => 'Dashboard',
                        'module' => 'backoffice',
                        'controller' => 'index'
                        //'action' => 'index'
                    ),
                    array(
                        'label' => 'Account Profile',
                        'module' => 'backoffice',
                        'controller' => 'profile'
                    )
                )
            ),
            array(
                'label' => 'Help Desk',
                'pages' => array(
                    array(
                        'label' => 'My Tickets',
                        'module' => 'backoffice',
                        'controller' => 'helpdesk',
                        /*'action' => 'index',*/
                        /*'params' => array(
                            'view' => 'mine'
                        )*/
                    ),
                    array(
                        'label' => 'Inbox',
                        'module' => 'backoffice',
                        'controller' => 'helpdesk',
                        'action' => 'provider-inbox'
                    )
                )
            ),
            array(
                'label' => 'System',
                'pages' => array(
                    array(
                        'label' => 'Groups',
                        'module' => 'backoffice',
                        'controller' => 'group'
                    ),
                    array(
                        'label' => 'Users',
                        'module' => 'backoffice',
                        'controller' => 'users'
                    ),
                    array(
                        'label' => 'Privileges',
                        'module' => 'backoffice',
                        'controller' => 'privilege'
                    ),
                    array(
                        'label' => 'Flags',
                        'module' => 'backoffice',
                        'controller' => 'flag'
                    ),
                    array(
                        'label' => 'Flippers',
                        'module' => 'backoffice',
                        'controller' => 'flipper'
                    ),
                    array(
                        'label' => 'Test page',
                        'module' => 'backoffice',
                        'controller' => 'dev',
                        'action' => 'index'
                    ),
                ),
            )
        );
        
        return $pages;
    }
    
    /**
     * Returns an array with all the pages that will be available for
     * the current user
     * 
     * @param array $data
     * @access protected
     * @return array
     */
    protected function _filter($data)
    {
        $userEmail = Zend_Auth::getInstance()->getIdentity();
        $filtered = array();
        
        foreach($data as $tab) 
        {
            $filteredPages = array();
            
            if(isset($tab['pages'])) 
            {
                foreach ($tab['pages'] as $page) 
                {
                    try 
                    {
                        if(!isset($page['action']))
                            $pageAction = 'index';
                        else
                            $pageAction = $page['action'];
                            
                        if (CC_FlagFlippers_Manager::isAllowed($userEmail, $page['controller'], $pageAction)) 
                        {
                            $filteredPages[] = $page;
                        }
                    } 
                    catch (Zend_Exception $e) 
                    {
                        // resource not yet registered, will be picked up by the CC_Controller
                        // when the user tries to access it. Add it for now for testing purposes
                        $filteredPages[] = $page;
                    }
                }
            }
            
            if(!empty($filteredPages)) 
            {
                $filteredTab = array('label' => $tab['label'], 'pages' => $filteredPages);
                $filtered[] = $filteredTab;
            }
        }
        
        return $filtered;
    }
    
    /**
     * Marks the current tab as active
     * 
     * @param arrat $pages 
     * @access protected
     * @return array
     */
    protected function _markActive($menu)
    {
        $controllerName = Zend_Registry::get('controllerName');
        
        foreach ($menu as $tabkey => $tab) 
        {
            if (isset($tab['pages'])) 
            {
                foreach ($tab['pages'] as $pagekey => $page) 
                {
                    if ($controllerName === $page['controller']) 
                    {
                        $menu[$tabkey]['pages'][$pagekey]['active'] = true;
                        $menu[$tabkey]['active'] = true;
                        break;
                    }
                }
            } 
            else 
            {
                if ($controllerName === $tab['controller']) 
                {
                    $menu[$tabkey]['active'] = true;
                }
            }
        }
        return $menu;
    }
    
    /**
     * Returns the navigation array
     * 
     * @access public
     * @return array
     */
    public function getNavigation ()
    {
        return $this->_navigation;
    }
}